package com.zzh.myapplication.activity;

import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import net.icarplus.car.R;
import net.icarplus.car.constant.UrlValues;
import net.icarplus.car.manager.BaseActivity;
import net.icarplus.car.manager.MainSelect;
import net.icarplus.car.tools.JsonUtil;
import net.icarplus.car.tools.LoadingUtil;
import net.icarplus.car.tools.S;
import net.icarplus.car.tools.SPUtils;
import net.icarplus.car.tools.T;
import net.icarplus.car.tools.ToolBarHelper;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Call;

/**
 * 项目名称：icarplus-car-android
 * 类描述：
 * 创建人：Gophy
 * 创建时间：16/3/8 22:24
 * 修改人：Gophy
 * 修改时间：16/3/8 22:24
 * 修改备注：
 */
public class LoginActivity extends BaseActivity implements View.OnClickListener {
    private Button btnLogin;
    private EditText etPhone;
    private EditText etPasswd;
    private LoadingUtil loadingUtil;
    private static String phone;
    private static String passwd;
    private Toolbar toolbar;
    private TextView tvForgetPasswd;
    private TextView tvFreeRegister;
    public static int REQUESTFORLOGIN = 1000;

    @Override
    public void setContent() {
        setContentView(R.layout.activity_login);
    }

    @Override
    public void initComponent() {
        btnLogin = (Button) findViewById(R.id.btn_login);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        ToolBarHelper.setNormalTitleAndBack(this, toolbar, getString(R.string.login));
        etPhone = (EditText) findViewById(R.id.login_name_edit);
        etPasswd = (EditText) findViewById(R.id.login_password_edit);
        tvForgetPasswd = (TextView) findViewById(R.id.login_forget_password_txt);
        tvFreeRegister = (TextView) findViewById(R.id.login_free_register_txt);
    }

    @Override
    public void setListener() {
        tvForgetPasswd.setOnClickListener(this);
        tvFreeRegister.setOnClickListener(this);
        btnLogin.setOnClickListener(this);
        this.loadingUtil = new LoadingUtil(this, getString(R.string.user_login));
    }

    @Override
    public void onAfterInit() {
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.btn_login:
                if (validInput()) {
                    login();
                }
                break;
            case R.id.login_forget_password_txt:
                intent.setClass(this, ForgetPasswdActivity.class);
                start_Activity(intent);
                break;
            case R.id.login_free_register_txt:
                intent.setClass(this, RegisterActivity.class);
                start_Activity(intent);
                break;
            default:
                break;
        }

    }

    private void login() {
        JSONObject json = new JSONObject();
        try {
            json.put("mobile", phone);
            json.put("password", passwd);
            json.put("loginChannel", "01");
            json.put("ipAddress", "127.0.0.1");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        this.loadingUtil.startShowLoading();
        OkHttpUtils.get().url( UrlValues.USER_LOGIN).
                addParams("input", json.toString())
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e) {
            }

            @Override
            public void onResponse(String response) {
                LoginActivity.this.loadingUtil.stopShowLoading();
                if (JsonUtil.isValid(response)) {
//                    SPUtils.saveString(LoginActivity.this, "password", passwd);
                    SPUtils.saveBoolean(LoginActivity.this, "isLogin", true);
                    if (MainActivity.fromOrder) {
                        MainActivity.mainPage = MainSelect.ORDER;
                    } else  {
                        MainActivity.mainPage = MainSelect.MINE;
                    }
                    setResult(REQUESTFORLOGIN);
                    finish();

                }
            }
        });
    }
    private boolean validInput() {
        phone = etPhone.getText().toString();
        passwd = etPasswd.getText().toString();

        SPUtils.saveString(this,"LoginInfo",phone);

        int index = 0;
        boolean isShow = false;
        if (!S.isEmpty(phone) && S.isMobileNumber(phone)) {
            index++;
        } else {
            if (!isShow) {
                T.showShort(getApplicationContext(), "请输入正确的手机号");
                isShow = true;
            }
        }
        if (S.isPwd(passwd)) {
            index++;
        } else {
            if (!isShow) {
                T.showShort(getApplicationContext(), "请输入6位以上数字或者字母组合成的密码");
                isShow = true;
            }
        }

        if (index == 2) {
            return true;
        }
        return false;
    }
}
